﻿using CarRental.DDL;
using CarRental.Helpers;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View.Account {
    public partial class EditUser : System.Web.UI.Page {
        DbAccess __dbAccess = new DbAccess();
        user __user = new user();
        string __email = "";
        protected void Page_Load(object sender, EventArgs e) {
            __email = Request.QueryString["email"];
            if (string.IsNullOrEmpty(__email) && Shared.LoginInfo.Authorized == true) {
                __email = Shared.LoginInfo.UserEmail;
                ddlUserStatus.Enabled = false;
            }

            if (!IsPostBack) {

                ddlUserType.DataSource = new CarRental.Helpers.OptionItems.UserTypeLst(false, Shared.LoginInfo.UserType);
                ddlUserType.DataValueField = "Key";
                ddlUserType.DataTextField = "Title";
                ddlUserType.DataBind();

                ddlUserStatus.DataSource = new CarRental.Helpers.OptionItems.UserStatusLst(false);
                ddlUserStatus.DataValueField = "Key";
                ddlUserStatus.DataTextField = "Title";
                ddlUserStatus.DataBind();

                __user = __dbAccess.GetUser().Where(w => w.email == __email).FirstOrDefault();
                if (__user != null) {
                    InitialRecord();
                }
            }
        }

        private void InitialRecord() {
            tbLoginName.Text = __user.login_name;
            tbPassword.Text = __user.password;
            ddlUserType.SelectedValue = __user.user_type;
            tbUserName.Text = __user.user_name;
            tbAddress.Text = __user.address;
            tbContactNo.Text = __user.contact_no;
            tbEmail.Text = __user.email;
            ddlUserStatus.SelectedValue = __user.status;
            tbRemark.Text = __user.remarks;


        }

        protected void btnEdit_Click(object sender, EventArgs e) {
            user _user = new user();
            _user = __dbAccess.GetUser().Where(w => w.email == __email).FirstOrDefault();
            _user.password = tbPassword.Text;
            _user.address = tbAddress.Text;
            _user.contact_no = tbContactNo.Text;
            _user.status = ddlUserStatus.SelectedValue;
            _user.remarks = tbRemark.Text;

            __dbAccess.UpdateUser(_user);
            MessageBox.Show("Update user success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Response.Redirect(ResolveUrl("~/View/Account/UserLst.aspx"));
        }

        protected void btnBack_Click(object sender, EventArgs e) {
            Response.Redirect(ResolveUrl("~/View/Account/UserLst.aspx"));
        }


    }
}